package com.miaosha.ms_02.config;

import com.miaosha.ms_02.domain.User;
import com.miaosha.ms_02.prefix.MiaoshaUserKey;
import com.miaosha.ms_02.redis.JedisService;
import com.miaosha.ms_02.service.MiaoshaUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description //TODO
 * @Author Administrator
 * @DATE 2020/9/18 22:17
 **/
@Service
public class UserArgumentResolver implements HandlerMethodArgumentResolver {

    @Autowired
    private JedisService jedisService;

    /**
     * @param methodParameter
     * @return boolean
     * @Description 支持处理的对象类型
     * 如果请求参数中有 User 对象的，则直接放入spring MVC请求参数中
     **/
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        Class<?> cls = methodParameter.getParameterType();
        return cls == User.class;
    }

    /**
     * @Description
     * @param methodParameter
     * @param modelAndViewContainer
     * @param nativeWebRequest
     * @param webDataBinderFactory
     * @return java.lang.Object
     **/
    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        //获取 request 和  response 对象
        HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
        HttpServletResponse response = nativeWebRequest.getNativeResponse(HttpServletResponse.class);
        //从 request 中获取请求数据
        String cookieValue = getCookieValue(request, MiaoshaUserService.COOKIE_NAME_TOKEN);
        String paramValue = request.getParameter(MiaoshaUserService.COOKIE_NAME_TOKEN);
        if (StringUtils.isEmpty(cookieValue) && StringUtils.isEmpty(paramValue)) {
            return "login";
        }
        String token = StringUtils.isEmpty(cookieValue) ? paramValue : cookieValue;
        return jedisService.get(MiaoshaUserKey.USERKEY_TOKEN_NAME, token, User.class);
    }

    /**
     * @param request 请求域
     * @param token   cookie 名称
     * @return java.lang.String
     * @Description 根据名称 key 从 cookie 中获取值
     **/
    private String getCookieValue(HttpServletRequest request, String token) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(token)) {
                return cookie.getValue();
            }
        }
        return null;
    }
}
